<?php

namespace app\admin\model;

use app\common\model\Block as BlockModel;

/**
 * 内容块管理器模型
 * Class Block
 * @package app\store\model
 */
class Block extends BlockModel
{
    /**
     * 获取内容块列表 
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);

    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['block_tag'])) {
            $this->error = '请输入自由块标签名';
            return false;
        }
        if (empty($data['content'])) {
            $this->error = '请输入内容块内容';
            return false;
        }
        return $this->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if (empty($data['free_block_id'])) {
            $this->error = '缺少free_block_id参数';
            return false;
        }
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        if (empty($data['article_content'])) {
            $this->error = '请输入内容块内容';
            return false;
        }
        return $this->exists(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete($free_block_id)
    {
        return $this->where(['free_block_id'=>$free_block_id])->delete();
    }

}